package com.example.duanlei417.mypro2.andoridp;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.util.Log;
import android.view.Display;
import android.view.DisplayCutout;
import android.view.View;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowManager;

import java.util.List;

/**
 * 刘海屏控制
 * 刘海屏幕适配方法：
 * 1.设置 LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES 模式
 * 2.设置沉浸式布局模式
 * 3.计算状态栏高度，进行布局； 如果有特殊UI要求，则可以使用DisplayCutout类去获取刘海屏的坐标，完成UI
 */
public class DisplayCutoutDemo {

    private static final String TAG = DisplayCutoutDemo.class.getSimpleName();

    private Activity mActivity;

    public DisplayCutoutDemo(Activity activity) {
        mActivity = activity;
    }

    /**
     * 沉浸式布局设置
     */
    public void openFullScreenModel() {

        mActivity.requestWindowFeature(Window.FEATURE_NO_TITLE);

        //刘海屏启用设置
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.layoutInDisplayCutoutMode =
                WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
        mActivity.getWindow().setAttributes(lp);

        View decorView = mActivity.getWindow().getDecorView();
        int systemUiVisibility = decorView.getSystemUiVisibility();
        int flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN;

        systemUiVisibility |= flags;
        mActivity.getWindow().getDecorView().setSystemUiVisibility(systemUiVisibility);
    }

    /**
     * 获取状态栏高度， 计算状态栏高度，在布局的时候，要刻意避开这个区域
     * @param context
     * @return
     */
    public int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height",
                "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelOffset(resourceId);
        }

        Log.d(TAG, "getStatusBarHeight: " + result);

        return result;
    }

    /**
     * 在刘海屏状态栏填充控件，获取刘海在屏幕具体位置，避开刘海
     *
     * 1.通过DisplayCutout类提供的API去获取刘海屏的坐标位置
     * 2.在布局的时候，因为已知刘海屏的坐标，因此控件可以很方便的避开这个位置
     */
    @TargetApi(28)
    public void controlView() {
        final View decorView = mActivity.getWindow().getDecorView();

        if (decorView != null) {
            Log.d(TAG, "controlView: sdk_int-------" + Build.VERSION.SDK_INT);
            Log.d(TAG, "controlView: codes_p-------" + Build.VERSION_CODES.P);


            decorView.post(new Runnable() {
                @Override
                public void run() {
                    WindowInsets windowInsets = decorView.getRootWindowInsets();
                    if (windowInsets != null) {
                        DisplayCutout displayCutout = windowInsets.getDisplayCutout();

                        List<Rect> rects = displayCutout.getBoundingRects();
                        if (rects == null || rects.size() == 0) {
                            Log.e(TAG, "不是刘海屏");
                        } else {
                            Log.e(TAG, "刘海屏数量:" + rects.size());
                            for (Rect rect : rects) {
                                //Rect(414, 0 - 666, 126)
                                Log.e(TAG, "刘海屏区域：" + rect);
                            }
                        }

                    }
                }
            });

        }
    }
}


